CREATE TRIGGER TGKH_01 ON KHACHHANG
FOR INSERT, UPDATE
AS
BEGIN
	DECLARE @GIOITINH VARCHAR(30)
	SET @GIOITINH = (SELECT GIOITINH FROM inserted)
	IF(@GIOITINH = 'NAM' OR @GIOITINH = 'NU')
	BEGIN
		RETURN
	END
	ELSE
		BEGIN
			RAISERROR('GIOI TINH PHAI LA NAM HOAC NU',16,1)
			ROLLBACK TRAN
			RETURN
		END
		
END



CREATE TRIGGER TGKH_02 ON KHACHHANG
FOR INSERT,UPDATE
AS
BEGIN
	DECLARE @SODT VARCHAR(15)
	DECLARE @COUNT INT
	SET @SODT = (SELECT SoDT FROM inserted)
	SET @COUNT = (SELECT COUNT(*) FROM KHACHHANG WHERE SoDT = @SODT)
	IF(@COUNT > 1)
	BEGIN
		RAISERROR('SO DIEN THOAI BI TRUNG',16,1)
		ROLLBACK TRAN
		RETURN
	END
END

CREATE TRIGGER TGKH_03 ON KHACHHANG
FOR INSERT,UPDATE
AS
BEGIN
	DECLARE @SOCMND VARCHAR(15)
	DECLARE @COUNT INT
	SET @SOCMND = (SELECT SoCMND FROM inserted)
	SET @COUNT = (SELECT COUNT(*) FROM KHACHHANG WHERE SoCMND = @SOCMND)
	IF(@COUNT > 1)
	BEGIN
		RAISERROR('SO CMND BI TRUNG',16,1)
		ROLLBACK TRAN
		RETURN
	END
END
